//
//  QuickDataLocalStorageTool.m
//  AiShangJinFuSalesman
//
//  Created by AiShangTong on 2017/6/10.
//  Copyright © 2018年 AiShangTong. All rights reserved.
//

#import "QuickDataLocalStorageTool.h"

NSString *const QuickDataLocalStorageToolUserType = @"QuickDataLocalStorageToolUserType";
NSString *const QuickDataLocalStorageToolUserName = @"QuickDataLocalStorageToolUserName";
NSString *const QuickDataLocalStorageToolHistoryRecordInfo = @"QuickDataLocalStorageToolHistoryRecordInfo";
NSString *const QuickDataLocalStorageToolLoadState = @"QuickDataLocalStorageToolLoadState";
NSString *const QuickDataLocalStorageToolIdentityCardInfoFront = @"QuickDataLocalStorageToolIdentityCardInfoFront";
NSString *const QuickDataLocalStorageToolIdentityCardInfoBack = @"QuickDataLocalStorageToolIdentityCardInfoBack";
NSString *const QuickDataLocalStorageToolGetVerificationCodeCountDown = @"GetVerificationCodeCountDown";
NSString *const QuickDataLocalStorageToolStartUserAPPTime = @"QuickDataLocalStorageToolStartUserAPPTime";

@implementation QuickDataLocalStorageTool




+(NSString *)libraryPath{
    
    NSArray *libraryPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    
    return [[libraryPath objectAtIndex:0] stringByAppendingPathComponent:@"Caches"];
}
+(NSString *)tmpPath{
    
    NSString *tmpPathStr = NSTemporaryDirectory();
    return tmpPathStr;
}




//扫描身份证信息存储
+ (BOOL)QuickDataLocalStorageToolIdentityCardInfoFrontWrite:(NSMutableDictionary *)identityInfo {
    
    NSString *pathStr = [QuickDataLocalStorageTool documentsPath];
    pathStr = [pathStr stringByAppendingPathComponent:[NSString stringWithFormat:@"QuickDataLocalStorageTool%@.plist",QuickDataLocalStorageToolIdentityCardInfoFront]];
    
    return [QuickDataLocalStorageTool writeDictionary:identityInfo toFilePath:pathStr];
}
+ (BOOL)QuickDataLocalStorageToolIdentityCardInfoBackWrite:(NSMutableDictionary *)identityInfo {
    
    NSString *pathStr = [QuickDataLocalStorageTool documentsPath];
    pathStr = [pathStr stringByAppendingPathComponent:[NSString stringWithFormat:@"QuickDataLocalStorageTool%@.plist",QuickDataLocalStorageToolIdentityCardInfoBack]];
    
    return [QuickDataLocalStorageTool writeDictionary:identityInfo toFilePath:pathStr];
}

//读取
+ (NSMutableDictionary *)QuickDataLocalStorageToolIdentityCardInfoBackRead {
    
    NSString *pathStr = [QuickDataLocalStorageTool documentsPath];
    pathStr = [pathStr stringByAppendingPathComponent:[NSString stringWithFormat:@"QuickDataLocalStorageTool%@.plist",QuickDataLocalStorageToolIdentityCardInfoBack]];
    
    return [QuickDataLocalStorageTool readDictionaryWithFilePath:pathStr];
}
+ (NSMutableDictionary *)QuickDataLocalStorageToolIdentityCardInfoFrontRead {
    
    NSString *pathStr = [QuickDataLocalStorageTool documentsPath];
    pathStr = [pathStr stringByAppendingPathComponent:[NSString stringWithFormat:@"QuickDataLocalStorageTool%@.plist",QuickDataLocalStorageToolIdentityCardInfoFront]];
    
    return [QuickDataLocalStorageTool readDictionaryWithFilePath:pathStr];
}

//验证码倒计时的读取与写入
+ (NSString *)GetVerificationCodeCountDown {
    
    NSString *pathStr = [QuickDataLocalStorageTool documentsPath];
    pathStr = [pathStr stringByAppendingPathComponent:[NSString stringWithFormat:@"QuickDataLocalStorageTool%@.plist",QuickDataLocalStorageToolGetVerificationCodeCountDown]];
    
    return [QuickDataLocalStorageTool readStringWithFilePath:pathStr];
}
+ (BOOL)WriteVerificationCodeCountDownWithStr:(NSString *)codeStr {
    
    NSString *pathStr = [QuickDataLocalStorageTool documentsPath];
    pathStr = [pathStr stringByAppendingPathComponent:[NSString stringWithFormat:@"QuickDataLocalStorageTool%@.plist",QuickDataLocalStorageToolGetVerificationCodeCountDown]];
    
    return [QuickDataLocalStorageTool writeString:codeStr toFilePath:pathStr];
}


//是否重新登录
+ (BOOL)isReLoadAgainWriteState:(NSString *)loadState {
    
    NSString *pathStr = [QuickDataLocalStorageTool documentsPath];
    pathStr = [pathStr stringByAppendingPathComponent:[NSString stringWithFormat:@"QuickDataLocalStorageTool%@.plist",QuickDataLocalStorageToolLoadState]];
    
    return [QuickDataLocalStorageTool writeString:loadState toFilePath:pathStr];
}
+ (NSString *)isReloadAgainReadState {
    
    NSString *pathStr = [QuickDataLocalStorageTool documentsPath];
    pathStr = [pathStr stringByAppendingPathComponent:[NSString stringWithFormat:@"QuickDataLocalStorageTool%@.plist",QuickDataLocalStorageToolLoadState]];
    
    return [QuickDataLocalStorageTool readStringWithFilePath:pathStr];
}

//进入APP时间记录
+ (BOOL)QuickDataLocalStorageToolStartUserAPPTimeWrite {
    
    NSString *pathStr = [QuickDataLocalStorageTool documentsPath];
    pathStr = [pathStr stringByAppendingPathComponent:[NSString stringWithFormat:@"QuickDataLocalStorageTool%@.plist",QuickDataLocalStorageToolStartUserAPPTime]];
    
    return [QuickDataLocalStorageTool writeString:[HFQCommonTools getSystemCurrentTime] toFilePath:pathStr];
}
+ (BOOL)QuickDataLocalStorageToolStartUserAPPTimeRead {
    
    NSString *pathStr = [QuickDataLocalStorageTool documentsPath];
    pathStr = [pathStr stringByAppendingPathComponent:[NSString stringWithFormat:@"QuickDataLocalStorageTool%@.plist",QuickDataLocalStorageToolStartUserAPPTime]];
    NSString *lastTime = [QuickDataLocalStorageTool readStringWithFilePath:pathStr];
    if (lastTime) {
        NSString *currentTime = [HFQCommonTools getSystemCurrentTime];
        
        if ([currentTime integerValue]-[lastTime integerValue] < 432000) {
            return NO;
        }else {
            return YES;
        }
    }
    return NO;
}


//搜索 记录
+ (BOOL)QuickDataLocalStorageToolHistoryRecordInfoWrite:(NSMutableArray *)recordInfoList withIdentifying:(NSString *)identifying {
    
    NSString *pathStr = [QuickDataLocalStorageTool documentsPath];
    pathStr = [pathStr stringByAppendingPathComponent:[NSString stringWithFormat:@"QuickDataLocalStorageTool%@%@.plist", QuickDataLocalStorageToolHistoryRecordInfo, identifying]];
    
    return [QuickDataLocalStorageTool writeArray:recordInfoList toFilePath:pathStr];
}
//搜索 记录 读取
+ (NSMutableArray *)QuickDataLocalStorageToolHistoryRecordInfoReadWithIdentifying:(NSString *)identifying {
    
    NSString *pathStr = [QuickDataLocalStorageTool documentsPath];
    pathStr = [pathStr stringByAppendingPathComponent:[NSString stringWithFormat:@"QuickDataLocalStorageTool%@%@.plist", QuickDataLocalStorageToolHistoryRecordInfo, identifying]];
    
    return [QuickDataLocalStorageTool readArrayWithFilePath:pathStr];
}


//用户名 记录
+ (BOOL)QuickDataLocalStorageToolUserInfoWrite:(NSMutableDictionary *)userInfo {
    
    NSString *pathStr = [QuickDataLocalStorageTool documentsPath];
    pathStr = [pathStr stringByAppendingPathComponent:[NSString stringWithFormat:@"QuickDataLocalStorageTool%@.plist",QuickDataLocalStorageToolUserName]];
    
    return [QuickDataLocalStorageTool writeDictionary:userInfo toFilePath:pathStr];
}
//用户名读取
+ (NSMutableDictionary *)QuickDataLocalStorageToolUserInfoRead {
    
    NSString *pathStr = [QuickDataLocalStorageTool documentsPath];
    pathStr = [pathStr stringByAppendingPathComponent:[NSString stringWithFormat:@"QuickDataLocalStorageTool%@.plist",QuickDataLocalStorageToolUserName]];
    
    return [QuickDataLocalStorageTool readDictionaryWithFilePath:pathStr];
}



////用户申请 类型 存储[mutDic setObject:@"1" forKey:@"orderClass"]
//+ (BOOL)QuickDataLocalStorageToolUserTypeWrite:(NSMutableDictionary *)userType {
//    
//    NSString *pathStr = [QuickDataLocalStorageTool documentsPath];
//    pathStr = [pathStr stringByAppendingPathComponent:[NSString stringWithFormat:@"QuickDataLocalStorageTool%@.plist",QuickDataLocalStorageToolUserType]];
//    
//    return [QuickDataLocalStorageTool writeDictionary:userType toFilePath:pathStr];
//}
////用户申请 类型 读取
//+ (NSMutableDictionary *)QuickDataLocalStorageToolUserTypeRead {
//    
//    NSString *pathStr = [QuickDataLocalStorageTool documentsPath];
//    pathStr = [pathStr stringByAppendingPathComponent:[NSString stringWithFormat:@"QuickDataLocalStorageTool%@.plist",QuickDataLocalStorageToolUserType]];
//    
//    return [QuickDataLocalStorageTool readDictionaryWithFilePath:pathStr];
//}



//沙河 document 路径；
+(NSString *)documentsPath {
    
    NSArray *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [documentsPath objectAtIndex:0];
}


//将一个字典写入文件
+ (BOOL)writeDictionary:(NSMutableDictionary *)dic toFilePath:(NSString *)filePath {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        [fileManager removeItemAtPath:filePath error:nil];
    }
    NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    return  [fileManager createFileAtPath:filePath contents:data attributes:nil];
}

//将一个数组写入文件
+ (BOOL)writeArray:(NSMutableArray *)arr toFilePath:(NSString *)filePath {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        [fileManager removeItemAtPath:filePath error:nil];
    }
    NSData *data = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:nil];
    return  [fileManager createFileAtPath:filePath contents:data attributes:nil];
}

//将一个字符串写入文件
+ (BOOL)writeString:(NSString *)str toFilePath:(NSString *)filePath {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        [fileManager removeItemAtPath:filePath error:nil];
    }
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    return  [fileManager createFileAtPath:filePath contents:data attributes:nil];
}

//从文件中读取字典
+ (NSMutableDictionary *)readDictionaryWithFilePath:(NSString *)filePath {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        NSData *data = [fileManager contentsAtPath:filePath];
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        return dic;
    } else {
        return nil;
    }
    
}

//从文件中读取数组
+ (NSMutableArray *)readArrayWithFilePath:(NSString *)filePath {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        NSData *data = [fileManager contentsAtPath:filePath];
        NSMutableArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        return arr;
    } else {
        return nil;
    }
}

//从文件中读取字符串
+ (NSString *)readStringWithFilePath:(NSString *)filePath {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if ([fileManager fileExistsAtPath:filePath]) {
        return [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    } else {
        return nil;
    }
}



//将一个字典写入文件
//+ (BOOL)writeBuyCarSelectDictionary:(NSMutableDictionary *)dic {
//    
//    
//    NSFileManager *fileManager = [NSFileManager defaultManager];
//    
//    NSString *private = [QuickDataLocalStorageTool documentsPath];
//    private = [private stringByAppendingPathComponent:[NSString stringWithFormat:@"selectedInfo%@.plist",userInfo]];
//    
//    
//    if ([fileManager fileExistsAtPath:private]) {
//        [fileManager removeItemAtPath:filePath error:nil];
//    }
//    if (!dic) {
//        return NO;
//    }
//    NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
//    return  [fileManager createFileAtPath:filePath contents:data attributes:nil];
//}


//从文件中读取字典
//+ (NSMutableDictionary *)readBuyCarSelectDictionaryWithFilePath {
//    
//    NSDictionary *dic = [[Mediator sharedInstance]BKMyUserInfo];
//    NSString *filePath = [BKSCBuyCarTool selectedInfoDocumentPath:dic[@"BKMyUserInfoGetUserIdKey"]];
//    NSFileManager *fileManager = [NSFileManager defaultManager];
//    if ([fileManager fileExistsAtPath:filePath]) {
//        NSData *data = [fileManager contentsAtPath:filePath];
//        if (data == nil) {
//            return [NSMutableDictionary dictionary];
//        }
//        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//        return dic;
//    } else {
//        
//        return [NSMutableDictionary dictionary];
//    }
//    
//}

@end
